#include <Arduino.h>
#include "wifi_ctrl.h"

const int wdtTimeout = 10000;  //time in ms to trigger the watchdog
hw_timer_t *timer    = NULL;

void IRAM_ATTR resetModule() 
{
  ets_printf("ESP32 HEAP1 %d\r\n", esp_get_free_heap_size());
  ets_printf("reboot\n");
  esp_restart();
}

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(115200);//串口初始化,测试用

  delay(10);

  //打印ESP32空的堆大小
  Serial.printf("ESP32 HEAP %d\r\n", esp_get_free_heap_size());
  Serial.printf("ESP32 PSRAM ");
  Serial.println(ESP.getPsramSize());

  // //看psram是否初始化成功
  // if(psramFound())
  // {
  //   Serial.printf("ESP32 PSRAM ok\r\n");
  // }
  // else
  // {
  //   Serial.printf("ESP32 PSRAM fail\r\n");
  // }

  Serial.printf("ESP32 HEAP %d\r\n", esp_get_free_heap_size());

  WiFi_Setup();

  Serial.printf("ESP32 HEAP %d\r\n", esp_get_free_heap_size());

  timer = timerBegin(0, 80, true);                  //timer 0, div 80
  timerAttachInterrupt(timer, &resetModule, true);  //attach callback
  timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
  timerAlarmEnable(timer);                          //enable interrupt

  Serial.printf("loop start\r\n");
}

void loop()
{
  // put your main code here, to run repeatedly:
  timerWrite(timer, 0); //reset timer (feed watchdog)

  WiFi_Loop();

  timerWrite(timer, 0); //reset timer (feed watchdog)
}